package com.automannn.atm_back.codegenerate.util;

import java.util.ResourceBundle;

/**
 * 代码资源工具
 * @author chenkh
 * @time 2020/6/27 14:10
 */
public class CodeResourceUtil {
    /**
     * 数据库资源配置
     */
    private static final ResourceBundle dataBaseResource = ResourceBundle.getBundle("jeecg/jeecg_database");
    /**
     * 配置资源
     */
    private static final ResourceBundle configResource = ResourceBundle.getBundle("jeecg/jeecg_config");
    /**
     * 数据库驱动
     */
    public static String DIVER_NAME = "com.mysql.jdbc.Driver";
    /**
     * 数据库地址
     */
    public static String URL = "jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=UTF-8";
    /**
     * 用户名
     */
    public static String USERNAME = "root";
    /**
     * 密码
     */
    public static String PASSWORD = "root";
    /**
     * 数据库名称
     */
    public static String DATABASE_NAME = "sys";
    /**
     * 数据库类型
     */
    public static String DATABASE_TYPE = "mysql";
    /**
     * 属性必需项
     */
    public static String JEECG_UI_FIELD_REQUIRED_NUM = "4";
    /**
     * 属性搜索项
     */
    public static String JEECG_UI_FIELD_SEARCH_NUM = "3";
    /**
     * 项目路径 todo: 更改
     */
    public static String project_path = "c:/workspace/jeecg";
    /**
     * webRoto路径
     */
    public static String web_root_package = "WebRoot";
    /**
     * 源码根路径
     */
    public static String source_root_package = "src";
    /**
     * 业务包
     */
    public static String bussiPackage = "sun";
    /**
     * 实体包
     */
    public static String entity_package = "entity";
    /**
     * ui前端包名
     */
    public static String page_package = "page";
    /**
     * 属性转换
     */
    public static boolean JEECG_FILED_CONVERT = true;
    /**
     * 模板路径
     */
    public static String FREEMARKER_CLASSPATH = "/jeecg/template";
    /**
     * 扩展模板路径
     */
    public static String FREEMARKER_CLASSPATH_USERDEFINED = "/jeecg/ext-template";
    /**
     * 服务包
     */
    public static String PACKAGE_SERVICE_STYLE = "service";
    /**
     * 项目风格
     */
    public static String PACKAGE_PROJECT_STYLE = "project";
    /**
     * 实体路径
     */
    public static String ENTITY_URL;
    /**
     * 页面路径
     */
    public static String PAGE_URL;

    public static String ENTITY_URL_INX;
    public static String PAGE_URL_INX;
    /**
     * 模板路径
     */
    public static String TEMPLATEPATH;
    /**
     * 代码路径
     */
    public static String CODEPATH;
    /**
     * jsp路径
     */
    public static String JSPPATH;
    /**
     * 表生成ID
     */
    public static String JEECG_GENERATE_TABLE_ID;
    /**
     * 页面属性
     */
    public static String JEECG_GENERATE_UI_FILTER_FIELDS;
    /**
     * 系统编码
     */
    public static String SYSTEM_ENCODING;

    public CodeResourceUtil() {
    }

    private void l() {
    }

    public static final String getDriverName() {
        return dataBaseResource.getString("driver_name");
    }

    public static final String getURL() {
        return dataBaseResource.getString("url");
    }

    public static final String getUsername() {
        return dataBaseResource.getString("username");
    }

    public static final String getPassword() {
        return dataBaseResource.getString("password");
    }

    public static final String getDatabaseName() {
        return dataBaseResource.getString("database_name");
    }

    public static final boolean getJeecgFieldConvert() {
        String s = configResource.getString("jeecg_filed_convert");
        return !s.toString().equals("false");
    }

    private static String getBussiPackage() {
        return configResource.getString("bussi_package");
    }

    public static final String getEntityPackage() {
        return configResource.getString("entity_package");
    }

    public static final String getPagePackage() {
        return configResource.getString("page_package");
    }

    public static final String getTemplatePath() {
        return configResource.getString("templatepath");
    }

    public static final String getSourceRootPackage() {
        return configResource.getString("source_root_package");
    }

    public static final String getWebRootPackage() {
        return configResource.getString("webroot_package");
    }

    public static final String getSystemEncoding() {
        return configResource.getString("system_encoding");
    }

    public static final String getJeecgGenerateTableId() {
        return configResource.getString("jeecg_generate_table_id");
    }

    public static final String getUiFilterFields() {
        return configResource.getString("ui_filter_fields");
    }

    public static final String getJeecgUiSearchFiledNum() {
        return configResource.getString("jeecg_ui_search_filed_num");
    }

    public static final String getJeecgUiFieldRequiredNum() {
        return configResource.getString("jeecg_ui_field_required_num");
    }

    public static String getProject_path() {
        String projp = configResource.getString("project_path");
        if (projp != null && !"".equals(projp)) {
            project_path = projp;
        }

        return project_path;
    }

    public static void setProjectPath(String project_path) {
        CodeResourceUtil.project_path = project_path;
    }

    static {
        DIVER_NAME = getDriverName();
        URL = getURL();
        USERNAME = getUsername();
        PASSWORD = getPassword();
        DATABASE_NAME = getDatabaseName();
        JEECG_FILED_CONVERT = getJeecgFieldConvert();
        SYSTEM_ENCODING = getSystemEncoding();
        TEMPLATEPATH = getTemplatePath();
        source_root_package = getSourceRootPackage();
        web_root_package = getWebRootPackage();
        bussiPackage = getBussiPackage();
        JEECG_GENERATE_TABLE_ID = getJeecgGenerateTableId();
        JEECG_GENERATE_UI_FILTER_FIELDS = getUiFilterFields();
        JEECG_UI_FIELD_SEARCH_NUM = getJeecgUiSearchFiledNum();
        if (URL.indexOf("mysql") < 0 && URL.indexOf("MYSQL") < 0) {
            if (URL.indexOf("oracle") < 0 && URL.indexOf("ORACLE") < 0) {
                if (URL.indexOf("postgresql") < 0 && URL.indexOf("POSTGRESQL") < 0) {
                    if (URL.indexOf("sqlserver") >= 0 || URL.indexOf("sqlserver") >= 0) {
                        DATABASE_TYPE = "sqlserver";
                    }
                } else {
                    DATABASE_TYPE = "postgresql";
                }
            } else {
                DATABASE_TYPE = "oracle";
            }
        } else {
            DATABASE_TYPE = "mysql";
        }

        source_root_package = source_root_package.replace(".", "/");
        web_root_package = web_root_package.replace(".", "/");
        String bussiPackageUrl = bussiPackage.replace(".", "/");
        ENTITY_URL = source_root_package + "/" + bussiPackageUrl + "/" + entity_package + "/";
        PAGE_URL = source_root_package + "/" + bussiPackageUrl + "/" + page_package + "/";
        ENTITY_URL_INX = bussiPackage + "." + entity_package + ".";
        PAGE_URL_INX = bussiPackage + "." + page_package + ".";
        CODEPATH = source_root_package + "/" + bussiPackageUrl + "/";
        JSPPATH = web_root_package + "/" + "webpage" + "/" + bussiPackageUrl + "/";
    }
}
